package client;

/**
 * Made by Jonas
 */
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;


import model.*;
import model.exceptions.AbstractException;
import model.exceptions.WrongUserSelectedException;
import model.exceptions.YouAreBusyException;
import net.miginfocom.swing.*;
import java.awt.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.rmi.*;
import java.util.*;

public class ClientMainFrame extends JFrame implements KeyListener,
		ActionListener {

	private JPanel contentPane;
	private ClientController controller;
	private JList userList;
	private DefaultListModel listModel;
	private TextField textField_1;
	private TextField textField;
	private TextArea textArea;
	private Button button, button_1;

	/**
	 * Create the frame.
	 */
	public ClientMainFrame(ClientController controller) {
		super("Main Frame");
		this.controller = controller;
		setBounds(100, 100, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[][]", "[][]"));

		Label label = new Label("Chat window");
		contentPane.add(label, "cell 0 0,grow");

		Label label_1 = new Label("Users");
		contentPane.add(label_1, "cell 1 0,grow");

		textArea = new TextArea();
		textArea.setPreferredSize(new Dimension(1000, 1000));
		textArea.setEditable(false);
		contentPane.add(textArea, "cell 0 1,grow");

		listModel = new DefaultListModel();
		userList = new JList(listModel);
		userList.addListSelectionListener(new userListSelectionListener());
		repaint();
		listModel.clear();

		contentPane.add(userList, "flowy,cell 1 1,grow");

		textField = new TextField();
		contentPane.add(textField, "cell 0 4,grow");
		textField.addKeyListener(this);

		button = new Button("Start Game");
		button.addActionListener(this);
		contentPane.add(button, "flowx,cell 1 4,grow");

		Label label_2 = new Label("Your opponent :");
		label_2.setBackground(UIManager.getColor("Button.background"));
		contentPane.add(label_2, "cell 1 1,growx,aligny bottom");

		textField_1 = new TextField();
		textField_1.setSize(new Dimension(0, 15));
		textField_1.setPreferredSize(new Dimension(0, 15));
		textField_1.setEditable(false);
		contentPane.add(textField_1, "cell 1 1,growx,aligny bottom");

		button_1 = new Button("High Score");
		button_1.addActionListener(this);
		contentPane.add(button_1, "cell 1 4,grow");

		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new FrameHandler());
		setVisible(false);
	}

	private class FrameHandler extends WindowAdapter {
		public void windowClosing(WindowEvent event) {
			controller.logoutPressed();
		}
	}

	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == button)
			try {
				controller.startPressed(textField_1.getText());
			} catch (AbstractException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, e.getText());
			}
		if (ae.getSource() == button_1)
			controller.highScorePressed();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode == 10) {
			controller.sendChat(textField.getText());
			textField.setText("");
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
	}

	private class userListSelectionListener implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent e) {
			textField_1.setText(userList.getSelectedValue().toString());
		}
	}

	public void takeAllUsers(ArrayList<String> list) {
		listModel.clear();
		Iterator<String> it = list.iterator();
		while (it.hasNext())
			listModel.addElement(it.next());
	}

	public void addUser(String name) {
		listModel.addElement(name);
	}

	public void removeUser(String name) {
		listModel.removeElement(name);
	}

	public void showChat(Message message) {
		textArea.append(message.toString() + "\n");
	}

	public void error(String string) {
		new JOptionPane(string);
	}

	public void serverClosed() {
		JOptionPane.showMessageDialog(null, "The server has closed. Close your application!");
	}

}
